const chalk = require('chalk')
const ora = require('ora')
import { LineBreak } from '@/constant';
import symbols from 'log-symbols';

/**
 * example: say something, such as: ${}
 */
export const replaceTemplateString = (str: string, content?: string | string[]) => {
  if (content) {
    if (Array.isArray(content)) {
      let index = 0;
      return str.replace(/\$\{.+\}/g, ($1, $2) => {
        const after = content[index];
        index++;
        return after;
      });
    } else {
      return str.replace(/\$\{.+\}/g, () => {
        return content;
      });
    }
  } else return str;
}
/**
 * example: VARCHAR(50) => VARCHAR
 */
export const typeFilter = (str: string) => {
  return str.replace(/(\(.+\))/g, ($1, $2) => {
    return $1.replace($2, '');
  })
}


export const log = (str: string, content?: string | string[]) => {
  console.log(LineBreak, symbols.success, chalk.green(replaceTemplateString(str, content)));
}
export const warn = (str: string, content?: string | string[]) => {
  console.log(LineBreak, symbols.warning, chalk.yellow(replaceTemplateString(str, content)));
}
export const error = (str: string, content?: string | string[]) => {
  console.log(LineBreak, symbols.error, chalk.red(replaceTemplateString(str, content)));
}

export const Ora = (content: string) => {
  const proce = ora(content);
  return proce;
}


export const upperFirstStrCase = (str: string) => str[0]?.toUpperCase() + str.slice(1, str.length).toLowerCase();